import { defineStore } from 'pinia'

export const useSystemStore = defineStore('system', {
  state: () => ({
    // 系统信息
    systemInfo: {
      statusBarHeight: 0,    // 状态栏高度
      navBarHeight: 0,       // 导航栏高度
      totalTopHeight: 0,     // 顶部总高度
      windowWidth: 0,        // 窗口宽度
      windowHeight: 0,       // 窗口高度
      platform: '',          // 平台信息
      model: ''              // 设备型号
    },
    menuButtonInfo: {         // 胶囊按钮信息
      width: 0,
      height: 0,
      top: 0,
      right: 0,
      bottom: 0,
      left: 0
    }
  }),

  actions: {
    // 初始化系统信息
    initSystemInfo() {
      try {
        // 获取系统信息
        const system = uni.getSystemInfoSync()
        
        // 获取胶囊按钮信息（微信小程序）
        let menuButton = {}
        try {
          menuButton = uni.getMenuButtonBoundingClientRect ? uni.getMenuButtonBoundingClientRect() : {}
        } catch (e) {
          console.warn('获取胶囊按钮信息失败:', e)
          // 降级处理：使用默认值
          menuButton = {
            width: 87,
            height: 32,
            top: system.statusBarHeight + 4,
            right: system.windowWidth - 10,
            bottom: system.statusBarHeight + 4 + 32,
            left: system.windowWidth - 10 - 87
          }
        }

        // 计算导航栏高度
        const navBarHeight = menuButton.height 
          ? menuButton.height + (menuButton.top - system.statusBarHeight) * 2
          : 44 // 默认导航栏高度

        // 计算顶部总高度
        const totalTopHeight = system.statusBarHeight + navBarHeight

        // 更新状态
        this.systemInfo = {
          statusBarHeight: system.statusBarHeight || 0,
          navBarHeight: navBarHeight,
          totalTopHeight: totalTopHeight,
          windowWidth: system.windowWidth || 0,
          windowHeight: system.windowHeight || 0,
          platform: system.platform || '',
          model: system.model || ''
        }

        this.menuButtonInfo = {
          width: menuButton.width || 0,
          height: menuButton.height || 0,
          top: menuButton.top || 0,
          right: menuButton.right || 0,
          bottom: menuButton.bottom || 0,
          left: menuButton.left || 0
        }

        console.log('系统信息初始化完成:', {
          statusBarHeight: this.systemInfo.statusBarHeight,
          navBarHeight: this.systemInfo.navBarHeight,
          totalTopHeight: this.systemInfo.totalTopHeight
        })

      } catch (error) {
        console.error('初始化系统信息失败:', error)
        // 使用默认值
        this.systemInfo = {
          statusBarHeight: 20,
          navBarHeight: 44,
          totalTopHeight: 64,
          windowWidth: 375,
          windowHeight: 667,
          platform: 'unknown',
          model: 'unknown'
        }
      }
    },

    // 获取自定义导航栏样式
    getCustomNavStyle() {
      return {
        height: `${this.systemInfo.totalTopHeight}px`,
        paddingTop: `${this.systemInfo.statusBarHeight}px`,
        boxSizing: 'border-box'
      }
    },

    // 获取状态栏高度
    getStatusBarHeight() {
      return this.systemInfo.statusBarHeight
    },

    // 获取导航栏高度
    getNavBarHeight() {
      return this.systemInfo.navBarHeight
    },

    // 获取顶部总高度
    getTotalTopHeight() {
      return this.systemInfo.totalTopHeight
    }
  }
})